package com.taotao.sys.service.impl;

import com.github.pagehelper.Page;
import com.google.common.base.Preconditions;
import com.taotao.common.Pagination;
import com.taotao.sys.mapper.SysUserAccountMapper;
import com.taotao.sys.model.po.SysUserAccount;
import com.taotao.sys.model.vo.user.UserAccountPageParam;
import com.taotao.sys.model.vo.user.UserAccountPageVO;
import com.taotao.sys.model.vo.user.UserAccountParam;
import com.taotao.sys.service.SysUserAccountService;
import com.taotao.util.ConvertUtils;
import com.taotao.util.EntityUtils;
import com.taotao.util.FastMyBatisUtils;
import com.taotao.util.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * <p>
 * 账号表服务接口实现类
 * </p>
 *
 * @author Fan Beibei
 * @version V1.0
 * @since 2023-04-08 19:57:22
 */
@Slf4j
@Service
public class SysUserAccountServiceImpl implements SysUserAccountService {
    @Value("${taotao.default-password:123456}")
    private String defaultPassword;
    @Resource
    private SysUserAccountMapper sysUserAccountMapper;


    @Override
    public SysUserAccount selectById(Long id) {
        return sysUserAccountMapper.getById(id);
    }

    @Override
    public Pagination<UserAccountPageVO> pageList(UserAccountPageParam param) {
        Preconditions.checkArgument(null != param);

        Page<SysUserAccount> page = sysUserAccountMapper.selectByPage(param, param.getPageNum(), param.getPageSize());
        return ConvertUtils.page2Pagination(page, new ConvertUtils.Convertor<SysUserAccount, UserAccountPageVO>() {
            @Override
            public UserAccountPageVO convert(SysUserAccount sysUserAccount) {
                UserAccountPageVO vo = new UserAccountPageVO();
                BeanUtils.copyProperties(sysUserAccount, vo);
                return vo;
            }
        });
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePwd(Long userId, String password) {
        Preconditions.checkArgument(null != userId);
        Preconditions.checkArgument(StringUtils.isNotBlank(password));

        SysUserAccount account = new SysUserAccount();
        account.setId(userId);
        account.setPassword(PasswordUtil.encrypt(password));
        EntityUtils.setPo2Field(userId, LocalDateTime.now(), account);
        sysUserAccountMapper.updateIgnoreNull(account);
    }

    @Override
    public void addOrUpdate(UserAccountParam param, Long userId) {
        Preconditions.checkArgument(null != userId);
        Preconditions.checkArgument(null != param);

        SysUserAccount account = new SysUserAccount();
        BeanUtils.copyProperties(param, account);
        if (null == param.getId()) {
            account.setPassword(PasswordUtil.encrypt(defaultPassword));
            EntityUtils.setPo5Field(userId, LocalDateTime.now(), account);
            sysUserAccountMapper.saveIgnoreNull(account);
        } else {
            account.setAccount(null);
            EntityUtils.setPo2Field(userId, LocalDateTime.now(), account);
            sysUserAccountMapper.updateIgnoreNull(account);
        }
    }

    @Override
    public void deleteByIds(Set<Long> ids, Long userId) {
        Preconditions.checkArgument(null != userId);
        Preconditions.checkArgument(!CollectionUtils.isEmpty(ids));
        sysUserAccountMapper.deleteByIds(ids, FastMyBatisUtils.newUpdateIdTime(LocalDateTime.now(), userId));
    }

    @Override
    public void changeStatus(Set<Long> ids, Integer status, Long userId) {
        Preconditions.checkArgument(null != userId);
        Preconditions.checkArgument(null != status);
        Preconditions.checkArgument(!CollectionUtils.isEmpty(ids));

        sysUserAccountMapper.updateStatus(ids,status,userId,LocalDateTime.now());
    }


    @Override
    public SysUserAccount queryByAccount(String account) {
        Preconditions.checkArgument(StringUtils.isNotBlank(account));

        return sysUserAccountMapper.selectByAccount(account);
    }


}
